<?php namespace system\model;
use houdunwang\model\Model;

class User extends Model{
	//数据表
	protected $table = "user";

	//允许填充字段
	protected $allowFill = [ ];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
	];

	//自动过滤
    protected $filter=[
        //[表单字段名,过滤条件,处理时间]
    ];

	//时间操作,需要表中存在created_at,updated_at字段
	protected $timestamps=false;
	public function login(){
		//非空验证
//		dd($_POST);die;
		Validate::make( [
			[ 'code', 'isnull','验证码不为空', 3 ],
		    ['username','isnull','用户名不为空'],
		    ['password','isnull','密码不为空']
		]);
		//验证验证码是否正确
		Validate::make( [
//			[ 'code', 'captcha', '验证码输入错误', 3 ]
		] );
		if(!$data=Db::table('user')->where('username',$_POST['username'])->where('password',Crypt::encrypt($_POST['password']))->first()){
//			echo 111;die;
			//$this->error='用户名或者密码错误';
			//p($this->error);die;
			$data=Db::table('user')->where('username',$_POST['username'])->where('password',Crypt::encrypt($_POST['password']))->first();
//            if($data['session']==1){
//                message('该用户正在登陆,请稍后','back','error');
//			}
			return ['valid'=>0,'message'=>'用户名或者密码错误'];
			//return false;
		}else{
			$data=Db::table('user')->where('username',$_POST['username'])->where('password',Crypt::encrypt($_POST['password']))->first();
//			if($data['session']==1){
//				message('该用户正在登陆,请稍后','back','error');
//			}
			Session::set('uid',$data['uid']);
			Session::set('username',$data['username']);
			//防止一个账号在多个地方登陆
			Db::table('user')->where("uid",$data['uid'])->update(['session'=>1]);
//			p(Session::get('uid'));die;
			return ['valid'=>1,'message'=>'登录成功'];
		}
	}



}


